package com.ycl.bookstore.controller;

import com.ycl.bookstore.DTO.CommentDTO;
import com.ycl.bookstore.DTO.ShowDTO;
import com.ycl.bookstore.common.Result;
import com.ycl.bookstore.pojo.MyMessage;
import com.ycl.bookstore.service.FriendService;
import com.ycl.bookstore.service.impl.MessageService;
import com.ycl.bookstore.utils.AliOssUtil;
import com.ycl.bookstore.utils.MyThreadLocal;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import static com.ycl.bookstore.utils.RedisConstants.FRIENDS_ISLIKE_KEY;

@RestController
@RequestMapping("/friends")
public class FriendsController {

    @Resource
    private MessageService messageService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private FriendService friendService;

    @PutMapping("/like/{id}")
    public Result isLike(@PathVariable("id") Long id){
        // id为文章id
        // 获取用户id
        Long userId = MyThreadLocal.getUser().getId();
        MyMessage message = new MyMessage(id, userId);
        // 向消息队列中发送消息
        messageService.sendMessageWithIsLike(message);
        // 判断redis缓存是否有用户点赞记录
        String key=FRIENDS_ISLIKE_KEY+id;
        Boolean res = stringRedisTemplate.opsForSet().isMember(key, userId.toString());
        return Result.success(res);
    }

    @GetMapping("/list")
    public Result getFriendList(){
        return friendService.getFriendList();
    }

    @PostMapping("/comment")
    public Result sendComment(@RequestBody CommentDTO commentDTO){
        return friendService.sendComment(commentDTO);
    }

    @PostMapping("/show")
    public Result showPost(@RequestParam("files") MultipartFile[] files,@RequestParam("title") String title){
        return friendService.showPost(files,title);
    }
}
